<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="账号" prop="account">
      <el-input v-model="form.account" maxlength="30" />
    </el-form-item>
    <el-form-item label="昵称" prop="realName">
      <el-input v-model="form.realName" maxlength="30" />
    </el-form-item>
    <el-form-item label="手机号码" prop="phone">
      <el-input v-model="form.phone" maxlength="11" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="form.email" maxlength="50" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>
  
<script>

import { submit } from "@/api/user";
import store from '@/store'
import { getToken, setToken, removeToken, setName } from '@/utils/auth'
export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      form: {},

    };
  },
  watch: {
    user: {
      handler(user) {
        if (user) {
          this.form = { id: user.id,account: user.account, phone: user.phone, realName: user.realName, email: user.email };
        }
      },
      immediate: true
    }
  },
  methods: {
    submit() {
      submit(this.form).then(res => {
        if(res.success){
          const tmpName = this.form.realName || this.form.account
          
          store.dispatch('user/setName', tmpName)
          // setName(tmpName)

        }
      })
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
  